//
//  XQChartView.m
//  XQChartDemo
//
//  Created by xq on 2024/6/15.
//

#import "XQChartView.h"
#import <Masonry/Masonry.h>
//#import <AAChartKit/AAGlobalMacro.h>
//#import <AAChartKit/AAChartKit.h>
//#import <AAChartKit/AASeriesElement.h>
#import <XYChart/XYChart.h>

@interface XQChartView () <XYChartDelegate, XYChartDataSource>

@property (nonatomic, strong) XYChart *chartView;

@property (nonatomic, strong) XYChartDataSourceItem *chartDS;

/// 选中下标
/// -1表示未选中
@property (nonatomic, assign) NSInteger selectIndex;

@end

@implementation XQChartView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //        CGFloat chartViewWidth  = self.view.frame.size.width;
        //        CGFloat chartViewHeight = self.view.frame.size.height - 250;
        
        self.selectIndex = -1;
        self.minY = 0;
        self.maxY = 100;
        
        self.chartView = [[XYChart alloc] initWithFrame:CGRectMake(0, 0, 0, 0) chartType:XYChartTypeLine];
        [self addSubview:self.chartView];
        
        [self.chartView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        // 禁止滚动
//        self.chartView.delegate = self;
        self.chartDS = [XYChartDataSourceItem new];
        self.chartView.delegate = self;
        self.chartView.dataSource = self;
        //        self.chartView.userInteractionEnabled = false;
        
        [self reloadWithRefresh:NO];
    }
    return self;
}

//- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [super touchesMoved:touches withEvent:event];
//    // wxq 通过 touch 下标，进行自己控制选择还是其他
//    for (UITouch *obj in event.allTouches.allObjects) {
//        if ([obj.view isKindOfClass:[XQChartView class]]) {
//            //            NSLog(@"touchesMoved %@ %@", touches, event);
//            NSLog(@"touchesMoved %@", NSStringFromCGPoint([obj locationInView:self]));
//        }
//    }
//}

/// 更新点位置
/// @param points CGPoint类型
- (void)refreshWithPoints:(NSArray *)points {
    self.points = points;
    [self refreshChartView];
}

- (void)refreshChartView {
    [self reloadWithRefresh:YES];
}

- (void)reloadWithRefresh:(BOOL)refresh {
    
    if (refresh) {
    }else {
    }
}

#pragma mark - XYChartDelegate

/**
 是否展示UIMenuController
 */
- (BOOL)chart:(XYChart *)chart shouldShowMenu:(NSIndexPath *)index {
    return false;
}

/**
 点击后的action，重载一般就不show UIMenuController了
 */
- (void)chart:(XYChart *)chart itemDidClick:(id<XYChartItem>)item {
    
}

/**
 line用于展示圆圈，bar用于柱形图的动画
 */
//- (CAAnimation *)chart:(XYChart *)chart clickAnimationOfIndex:(NSIndexPath *)index;

#pragma mark - XYChartDataSource

/**
 多少条并行对比数据，折线图表现多条线，柱状图表现一列中有几条柱状图
 */
- (NSUInteger)numberOfSectionsInChart:(XYChart *)chart {
    return 10;
}

/**
 完整的周期内，数据的个数，横向列数
 */
- (NSUInteger)numberOfRowsInChart:(XYChart *)chart {
    return 10;
}

/**
 x坐标的标题
 */
//- (NSAttributedString *)chart:(XYChart *)chart titleOfRowAtIndex:(NSUInteger)index;

/**
 x坐标的标题
 */
//- (NSAttributedString *)chart:(XYChart *)chart titleOfSectionAtValue:(CGFloat)sectionValue;

/**
 index下的数据模型
 */
//- (id<XYChartItem>)chart:(XYChart *)chart itemOfIndex:(NSIndexPath *)index

/**
 标记y轴方向高亮区间
 */
//- (XYRange)visibleRangeInChart:(XYChart *)chart;

/**
 y轴方向分段，5就分5段
 */
//- (NSUInteger)numberOfLevelInChart:(XYChart *)chart {
//    return 5;
//}

/**
 横向一栏的宽度
 */
//- (CGFloat)rowWidthOfChart:(XYChart *)chart {
//    return 50;
//}

/**
 自适应平均分横向栏目的宽度
 */
//- (BOOL)autoSizingRowInChart:(XYChart *)chart {
//    return YES;
//}
//
//- (void)selectPointChangeY:(CGFloat)value {
//}

@end
